<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
  <head>
    <title>Specification of Our C Language</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta name="generator" content="FreshHTML v1.00">
    <meta name="generated" content="2010-04-16 11:15:01 ">
    <meta name="author" content="Zhong Zhuang">
    <meta name="description">
    <meta name="keywords">
    <style type="text/css">
<!--/*-->
<![CDATA[
  html { font-family: Times, serif; font-size: 12pt; }
  .title  { text-align: center; }
  .todo   { color: red; }
  .done   { color: green; }
  .tag    { background-color: #add8e6; font-weight:normal }
  .target { }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  p.verse { margin-left: 3% }
  pre {
	border: 1pt solid #AEBDCC;
	background-color: #F3F5F7;
	padding: 5pt;
	font-family: courier, monospace;
        font-size: 90%;
        overflow:auto;
  }
  table { border-collapse: collapse; }
  td, th { vertical-align: top; }
  dt { font-weight: bold; }
  div.figure { padding: 0.5em; }
  div.figure p { text-align: center; }
  .linenr { font-size:smaller }
  .code-highlighted {background-color:#ffff00;}
  .org-info-js_info-navigation { border-style:none; }
  #org-info-js_console-label { font-size:10px; font-weight:bold;
                               white-space:nowrap; }
  .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
                                 font-weight:bold; }
  ]]>
    </style>
    <script type="text/javascript">
<!--/*-->
      <![CDATA[
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;     
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
]]>
    </script>
  </head>
  <body>
    <h2>Specification of Our C Language</h2>
    <p>See <a href="#sec-5">Examples</a></p>
    <div id="outline-container-1" class="outline-2">
      <h2 id="sec-1"><span class="section-number-2">1</span> Notation</h2>
    </div>
    <p>We will use following noations in sections below.</p>
    <dl>
      <dt>
        Alternative 
        <dd>
          a <font color="green">|</font> b means "a <b>or</b> b" 
          <dt>
            Optional 
            <dd>
              <font color="green">[</font>a<font color="green">]</font> means either a or epsilon(empty
              sequence), so <font color="green">[</font>a<font color="green">]</font> equals to
              "a <font color="green">|</font> epsilon" 
              <dt>
                Repetition 
                <dd>
                  <font color="green">{</font>a<font color="green">}</font> means "epsilon <font color="green">|</font>
                  a <font color="green">|</font> aa <font color="green">|</font> aaa <font color="green">|</font>
                  &hellip;" 
                </dd>
              </dt>
            </dd>
          </dt>
        </dd>
      </dt>
    </dl>
    <div id="outline-container-2" class="outline-2">
      <h2 id="sec-2"><span class="section-number-2">2</span> Lexical Rule</h2>
    </div>
    <div id="text-2" class="outline-text-2">
      <table border="2" rules="groups" cellspacing="0" cellpadding="6" frame="hsides">
        <caption></caption>
        <colgroup>
          <col align="left">
          <col align="left">
          <col align="left">
        </colgroup>
        <tbody>
          <tr>
            <td><i>letter</i></td>
            <td>::=</td>
            <td>a <font color="green">|</font> b <font color="green">|</font> &hellip; <font color="green">|</font>
            z <font color="green">|</font> A <font color="green">|</font> B <font color="green">|</font>
            &hellip; <font color="green">|</font> Z</td>
          </tr>
          <tr>
            <td><i>digit</i></td>
            <td>::=</td>
            <td>0 <font color="green">|</font> 1 <font color="green">|</font> &hellip; <font color="green">|</font>
            9</td>
          </tr>
          <tr>
            <td><b>id</b></td>
            <td>::=</td>
            <td><i>letter</i> <font color="green">{</font><i>letter</i> <font color="green">|</font>
            <i>digit</i> <font color="green">|</font> _<font color="green">}</font></td>
          </tr>
          <tr>
            <td><b>intconst</b></td>
            <td>::=</td>
            <td><i>digit</i> <font color="green">{</font><i>digit</i><font color="green">}</font></td>
          </tr>
          <tr>
            <td><b>stringconst</b></td>
            <td>::=</td>
            <td><strong>"</strong><font color="green">{</font>any printable ASCII character<font color="green">}</font><strong>"</strong></td>
          </tr>
        </tbody>
      </table>
    </div>
    <p><i>Comments</i> are as in C, either a sequence started by / &lowast; and ended
    by &lowast; / or a line started by //</p>
    <div id="outline-container-3" class="outline-2">
      <h2 id="sec-3"><span class="section-number-2">3</span> Syntatic Rule</h2>
    </div>
    <div id="outline-container-3.1" class="outline-3">
      <h3 id="sec-3.1"><span class="section-number-3">3.1</span> Grammar</h3>
    </div>
    <div class="outline-text-3">
      <table border="2" rules="groups" cellspacing="0" cellpadding="6" frame="hsides">
        <caption></caption>
        <colgroup>
          <col align="left">
          <col align="left">
          <col align="left">
        </colgroup>
        <tbody>
          <tr>
            <td><i>prog</i></td>
            <td>:</td>
            <td><font color="green">{</font><i>funcdecl</i><font color="green">}</font> <font color="green">{</font><i>structdecl</i><font color="green">}</font>
            <font color="green">{</font><i>func</i><font color="green">}</font></td>
          </tr>
          <tr>
            <td><i>funcdecl</i></td>
            <td>:</td>
            <td><i>type</i> <font color="green">[</font>*<font color="green">]</font> <b>id</b>
            "<strong>(</strong>" <font color="green">[</font> <i>vardecl</i> <font color="green">{</font>"<strong>,</strong>"<i>vardecl</i><font color="green">}[</font>"<strong>,&hellip;</strong>"<font color="green">]]</font>
            "<strong>);</strong>"</td>
          </tr>
          <tr>
            <td><i>structdecl</i></td>
            <td>:</td>
            <td><b>struct id</b> "<strong>{</strong>" <font color="green">{</font><i>vardecl</i>"<strong>;</strong>"<font color="green">}</font>
            "<strong>}</strong>;"</td>
          </tr>
          <tr>
            <td><i>type</i></td>
            <td>:</td>
            <td><b>char</b> <font color="green">|</font> <b>int</b> <font color="green">|</font>
            <b>void</b> <font color="green">|</font> <b>struct id</b></td>
          </tr>
          <tr>
            <td><i>vardecl</i></td>
            <td>:</td>
            <td><i>type</i> <font color="green">[</font>*<font color="green">]</font> <b>id</b></td>
          </tr>
          <tr>
            <td><i>func</i></td>
            <td>:</td>
            <td><i>type</i> <font color="green">[</font>*<font color="green">]</font> <b>id</b>
            "<strong>(</strong>" <font color="green">[</font> <i>vardecl</i> <font color="green">{</font>"<strong>,</strong>"<i>vardecl</i><font color="green">}]</font>
            "<strong>)</strong>" <i>block</i></td>
          </tr>
          <tr>
            <td><i>block</i></td>
            <td>:</td>
            <td>"<strong>{</strong>" <font color="green">{</font><i>vardecl</i>"<strong>;</strong>"<font color="green">}</font>
            <font color="green">{</font><i>stmt</i><font color="green">}</font> "<strong>}</strong>"</td>
          </tr>
          <tr>
            <td><i>stmt</i></td>
            <td>:</td>
            <td><b>if</b> "<strong>(</strong>" <i>expr</i> "<strong>)</strong>" <i>block</i>
            {<b>else</b> <i>block</i>}</td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><b>while</b> "<strong>(</strong>" <i>expr</i> "<strong>)</strong>" <i>block</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><b>for</b> "<strong>(</strong>" <i>assign</i>"<strong>;</strong>"<i>expr</i>"<strong>;</strong>"<i>assign</i>"<strong>)</strong>"
            <i>block</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><b>return</b> <font color="green">[ </font><i>expr</i> <font color="green">]</font>"<strong>;</strong>"</td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>assign</i>"<strong>;</strong>"</td>
          </tr>
	  <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>expr</i>"<strong>;</strong>"</td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>block</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td>"<strong>;</strong>"</td>
          </tr>
          <tr>
            <td><i>assign</i></td>
            <td>:</td>
            <td><i>lval</i> "<strong>=</strong>" <i>expr</i></td>
          </tr>
          <tr>
            <td><i>lval</i></td>
            <td>:</td>
            <td><b>id</b> <font color="green">|</font> "<strong>*</strong>"<b>id</b> <font color="green">|</font>
            <b>id</b>"<strong>.</strong>"<b>id</b><font color="green"> |</font> <b>id</b>"<strong>-&gt;</strong>"<b>id</b>
            <font color="green">|</font> <b>id</b>"<strong>[</strong>"<i>intconst</i>"<strong>]</strong>"</td>
          </tr>
          <tr>
            <td><i>expr</i></td>
            <td>:</td>
            <td>"<strong>-</strong>" <i>expr</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td>"<strong>!</strong>" <i>expr</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><b>id</b> "<strong>(</strong>" <font color="green">[ </font><i>expr</i> <font color="green">{</font>"<strong>,</strong>"<i>expr</i><font color="green">}]</font>
            "<strong>)</strong>"</td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>expr binop expr</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>expr relop expr</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>expr logicop expr</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><i>lval</i></td>
          </tr>
          <tr>
            <td></td>
            <td><font color="green">|</font></td>
            <td><b>intconst</b> <font color="green">| </font><b>stringconst</b></td>
          </tr>
          <tr>
            <td><i>binop</i></td>
            <td>:</td>
            <td>+ <font color="green">|</font> - <font color="green">|</font> * <font color="green">|</font>
            /</td>
          </tr>
          <tr>
            <td><i>relop</i></td>
            <td>:</td>
            <td>&gt;= <font color="green">|</font> &gt; <font color="green">|</font> &lt; <font color="green">|</font>
            &lt;= <font color="green">|</font> != <font color="green">|</font> ==</td>
          </tr>
          <tr>
            <td><i>logicop</i></td>
            <td>:</td>
            <td>
            <p>&amp;&amp; <font color="green">|</font> ||</p>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <h3 id="sec-3.2" class="outline-3"><span class="section-number-3">3.2</span> Operator
    Associativty and Precendence</h3>
    <p>TODO</p>
    <div id="outline-container-4" class="outline-2">
      <h2 id="sec-4"><span class="section-number-2">4</span> Typing Rule</h2>
    </div>
    <p>TODO</p>
    <div id="outline-container-5" class="outline-2">
      <h2 id="sec-5"><span class="section-number-2">5</span> Examples</h2>
    </div>
    <div id="text-5" class="outline-text-2">
<pre class="src src-c"><span style="color: #b22222;">// </span><span style="color: #b22222;">I'm comment
</span><span style="color: #b22222;">/* </span><span style="color: #b22222;">We are 
   comments 
   too!
</span><span style="color: #b22222;">*/</span>
<span style="color: #b22222;">// </span><span style="color: #b22222;">func decls
</span><span style="color: #228b22;">int</span> <span style="color: #0000ff;">printf</span>(<span style="color: #228b22;">char</span> * <span style="color: #b8860b;">format</span>, ...);
<span style="color: #228b22;">void</span>* <span style="color: #0000ff;">malloc</span>(<span style="color: #228b22;">int</span> <span style="color: #b8860b;">size</span>);
<span style="color: #b22222;">// </span><span style="color: #b22222;">struct decls
</span><span style="color: #a020f0;">struct</span> <span style="color: #228b22;">listnode</span> {
  <span style="color: #228b22;">int</span> <span style="color: #b8860b;">val</span>;
  <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">listnode</span> * <span style="color: #b8860b;">next</span>;
}
<span style="color: #b22222;">//</span><span style="color: #b22222;">funcs
</span><span style="color: #228b22;">int</span> <span style="color: #0000ff;">main</span>() {
  <span style="color: #228b22;">int</span> <span style="color: #b8860b;">i</span>;
  <span style="color: #228b22;">int</span> <span style="color: #b8860b;">k</span>;
  <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">listnode</span> * <span style="color: #b8860b;">head</span>;
  <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">listnode</span> * <span style="color: #b8860b;">prev</span>;
  prev = <span style="color: #5f9ea0;">NULL</span>;
  i=5;
  <span style="color: #a020f0;">for</span>(k=0;k&lt;i;k=k+1) {
    head=malloc(8);
    head-&gt;val = k;
    head-&gt;next= prev;
    prev = head;
  }
  <span style="color: #a020f0;">return</span> head;
}
</pre>
     
    </div>
  </body>
</html>
